import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SortNodeHierarchy')
export class SortNodeHierarchy extends Component {
    @property(String)
    node_name = ""

    start() {
        //this.on_sort(this.node_name);
    }

    /** 根据node位置高低 排序node层级 */
    on_posY_sort(_name:string){
        if(_name.length <= 0)return;

        let temp_nodes = [];
        this.node.children.forEach(element => {
            if(element.name.length >= this._name.length){
                if(element.name.substring(0,this._name.length) == this._name){
                    const y = element.getWorldPosition().y;
                    let t_node = {node:element , posY:y}
                    temp_nodes.push(t_node)
                }
            }
        })

        temp_nodes.sort((a,b)=>b.posY - a.posY)

        for (let i = 0; i < temp_nodes.length; i++) {
            const element:Node = temp_nodes[i].node;
            element.setSiblingIndex(this.node.children.length - 1)
        }
    }
}


